////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2008 Chris Colinsky
// All Rights Reserved
//
// version	0.4
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

import sugarcookie.utils.ObjectUtil;
import sugarcookie.collections.ArrayCollection;
import sugarcookie.events.EventController;
import sugarcookie.dataHandlers.IDataChannel;

class com.cookiejar.dataHandlers.FlashVarsHandler extends EventController implements IDataChannel {
	private static var _instance:FlashVarsHandler = null;
	private var _vars:ArrayCollection;
	
	private function FlashVarsHandler() {
		super(this);
		
		_vars = new ArrayCollection();
	}
	
	public static function getInst():FlashVarsHandler {
		return FlashVarsHandler(_instance?_instance:_instance = new FlashVarsHandler());
	}
	
	public function init():Void {
		getFlashVars();
	}
	
	public function kill():Void {
		
	}
	
	private function getFlashVars():Void {
		var status:Boolean = false;
		for (var i in _level0) {
			if (i.indexOf("_fv")>0) {
				status = true;
				var key = i.substring(0,i.indexOf("_fv"));
				_vars.addObjWithKey(key,_level0[i]);
			}
		}
		
		if (!status) postEvent("onDataChannelError","FlashVars", false,true);
		else postEvent("onDataChannelReturn","FlashVars", true,true,_vars);
	}
	
	public function get vars():ArrayCollection {
		return _vars;
	}
}